package com.probox.common.entity.service.customer.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.entity.domain.customer.CustomerFundStore;
import com.probox.common.entity.mapper.customer.CustomerFundStoreMapper;
import com.probox.common.entity.service.customer.ICustomerFundStoreService;
import com.probox.common.entity.vo.customer.CustomerFundStoreVo;
import com.probox.common.entity.vo.customer.StoreFundItem;
import com.probox.common.entity.vo.customerService.CustomerServiceBuyRecordVo;
import org.bouncycastle.asn1.icao.CscaMasterList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 客户资金Service业务层处理
 *
 * @author usoft
 * @date 2022-10-06
 */
@Service
@Transactional
public class CustomerFundStoreServiceImpl extends ServiceImpl<CustomerFundStoreMapper, CustomerFundStore> implements ICustomerFundStoreService {
    @Autowired
    private CustomerFundStoreMapper customerFundStoreMapper;

    /**
     * 查询客户各个门店的资金信息
     *
     * @param customerUid
     * @return
     */
    @Override
    public List<CustomerFundStoreVo> listFundStore(Long customerUid) {
        return customerFundStoreMapper.listFundStore(customerUid);
    }

    @Override
    public List<CustomerFundStoreVo> sumStoreFundList(Long storeId) {
        return customerFundStoreMapper.sumStoreFundList(storeId);
    }


    @Override
    public Integer listFundStoreTotal(Long customerUid) {
        return customerFundStoreMapper.listFundStoreTotal(customerUid);
    }

    /**
     * 查询客户资金
     *
     * @param fundUid 客户资金ID
     * @return 客户资金
     */
    @Override
    public CustomerFundStore selectCustomerFundStoreById(Long fundUid) {
        return this.getById(fundUid);
    }

    @Override
    public CustomerFundStore selectCustomerFund(Long storeId, Long customerId) {
        QueryWrapper<CustomerFundStore> queryWrapper = Wrappers.query();
        queryWrapper.eq("CUSTOMER_UID", customerId);
        queryWrapper.eq("STORE_ID", storeId);
        queryWrapper.last(" limit 1 ");
        return customerFundStoreMapper.selectOne(queryWrapper);
    }

    /**
     * 查询客户资金列表
     *
     * @param customerFundStore 客户资金
     * @return 客户资金
     */
    @Override
    public List<CustomerFundStore> queryList(CustomerFundStore customerFundStore) {
        QueryWrapper<CustomerFundStore> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(customerFundStore.getFundUid())) && customerFundStore.getFundUid() != null) {
            queryWrapper.eq("FUND_UID", customerFundStore.getFundUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerFundStore.getCustomerUid())) && customerFundStore.getCustomerUid() != null) {
            queryWrapper.eq("CUSTOMER_UID", customerFundStore.getCustomerUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerFundStore.getStoreId())) && customerFundStore.getStoreId() != null) {
            queryWrapper.eq("STORE_ID", customerFundStore.getStoreId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerFundStore.getAllBalance())) && customerFundStore.getAllBalance() != null) {
            queryWrapper.eq("ALL_BALANCE", customerFundStore.getAllBalance());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerFundStore.getBalance())) && customerFundStore.getBalance() != null) {
            queryWrapper.eq("BALANCE", customerFundStore.getBalance());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerFundStore.getAllRedBag())) && customerFundStore.getAllRedBag() != null) {
            queryWrapper.eq("ALL_RED_BAG", customerFundStore.getAllRedBag());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerFundStore.getRedBag())) && customerFundStore.getRedBag() != null) {
            queryWrapper.eq("RED_BAG", customerFundStore.getRedBag());
        }
        return customerFundStoreMapper.selectList(queryWrapper);
    }

    /**
     * 新增客户资金
     *
     * @param customerFundStore 客户资金
     * @return 结果
     */
    @Override
    public boolean insertCustomerFundStore(CustomerFundStore customerFundStore) {

        return this.save(customerFundStore);

    }

    /**
     * 修改客户资金
     *
     * @param customerFundStore 客户资金
     * @return 结果
     */
    @Override
    public boolean updateCustomerFundStore(CustomerFundStore customerFundStore) {

        return this.updateById(customerFundStore);
    }

    /**
     * 批量删除客户资金
     *
     * @param fundUids 需要删除的客户资金ID
     * @return 结果
     */
    @Override
    public boolean deleteCustomerFundStoreByIds(Long[] fundUids) {
        return this.removeByIds(Arrays.asList(fundUids));
    }

    @Override
    public List<StoreFundItem> getStoreFundByCustomerId(Long customerId) {
        return customerFundStoreMapper.getStoreFundByCustomerId(customerId);
    }

    @Override
    public List<CustomerFundStore> customerStoreFundList(Long storeId) {
        return customerFundStoreMapper.customerStoreFundList(storeId);
    }


    /**
     * 增加/修改客户门店资金表
     *
     * @param record
     */
    @Override
    public void saveOrUpdateFundStore(CustomerServiceBuyRecordVo record) {
        /**
         * 先查询是否存在
         */
        CustomerFundStore customerFundStore = selectCustomerFund(record.getStoreUid(), record.getCustomerId());
        if (ObjectUtil.isEmpty(customerFundStore)) {
            customerFundStore = new CustomerFundStore();
            customerFundStore.setCustomerUid(record.getCustomerId());
            customerFundStore.setStoreId(record.getStoreUid());
            customerFundStore.setAllBalance(record.getRetreatByBalance());
            customerFundStore.setBalance(record.getRetreatByBalance());
            customerFundStore.setAllRedBag(record.getRetreatByRedBag());
            customerFundStore.setRedBag(record.getRetreatByRedBag());
            customerFundStore.setCreateTime(DateUtil.date());
            this.save(customerFundStore);
        } else {
            customerFundStore.setAllBalance(customerFundStore.getAllBalance() + record.getRetreatByBalance());
            customerFundStore.setBalance(customerFundStore.getBalance() + record.getRetreatByBalance());
            customerFundStore.setAllRedBag(customerFundStore.getAllRedBag() + record.getRetreatByRedBag());
            customerFundStore.setRedBag(customerFundStore.getRedBag() + record.getRetreatByRedBag());
            customerFundStore.setUpdateTime(DateUtil.date());
            this.updateById(customerFundStore);
        }
    }

}
